package com.glodon.paas.document.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.ProjectRole;
import com.glodon.paas.document.service.ProjectService;
import com.glodon.paas.document.web.util.SecurityUtils;

/**
 * 项目角色信息 操作
 * @author wangchen
 */
@Controller
@RequestMapping("/role")
public class ProjectRoleController {

    public static final Logger LOGGER = LoggerFactory.getLogger(ProjectRoleController.class);
    @Autowired
    private ProjectService     projectService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ProjectRole createRole(@RequestBody ProjectRole role, ModelMap model) {
        return projectService.createRole(SecurityUtils.getCurrentUser().getId(), role.getProjectId(), role.getRoleName());
    }
    
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public ProjectRole updateRole(@RequestBody ProjectRole role, ModelMap model) {
        return projectService.updateRole(SecurityUtils.getCurrentUser().getId(), role.getId(), role.getRoleName());
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public void getRoles(@RequestParam("projectId") String projectId, ModelMap model) {
            model.addAttribute(StringConst.ITEMS, projectService.getProjectRoles(SecurityUtils.getCurrentUser().getId(), projectId));
    }
    
    @RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
    public void deleteRole(@PathVariable("roleId") String roleId, ModelMap model) {
            projectService.deleteRole(SecurityUtils.getCurrentUser().getId(), roleId);
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
    }

}
